package com.example.zg3_park.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.lib_base.entity.NoticeEntityResultItem
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ItemNoticeBinding

class NoticeAdapter: PagingDataAdapter<NoticeEntityResultItem,NoticeAdapter.NoticeViewHolder>(diff){

    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null

    //伴生对象
    companion object{
        val diff = object :ItemCallback<NoticeEntityResultItem>() {
            override fun areItemsTheSame(
                oldItem: NoticeEntityResultItem,
                newItem: NoticeEntityResultItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: NoticeEntityResultItem,
                newItem: NoticeEntityResultItem
            ): Boolean {
                //内容是否相同
                return oldItem.id == newItem.id
            }

        }
    }

    class NoticeViewHolder(view:View):ViewHolder(view) {
        val binding = DataBindingUtil.bind<ItemNoticeBinding>(view)
    }

    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        //赋值
        holder.binding?.bean = getItem(position)

        val item = getItem(position)
        //点击事件
        holder.binding?.bean = item
        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position,item!!)
            return@setOnClickListener
        }
        holder.itemView.setOnLongClickListener {
            this.mOnItemLongClickListener?.onItemLongClick(item!!)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeViewHolder {
        //创建viewHolder
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_notice, parent, false)
        return NoticeViewHolder(view)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mOnItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
}


interface OnItemClickListener {
    fun onItemClick(position: Int,item:NoticeEntityResultItem)
}

interface OnItemLongClickListener {

    fun onItemLongClick(item: NoticeEntityResultItem): Boolean
}




